package hr.dao;

import hr.to.DepartmentTO;
import hr.to.EmployeeTO;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class EmployeeDAO {
	private static EmployeeDAO instance = new EmployeeDAO();
	private SqlMapClient sqlMap;
	private EmployeeDAO(){
		try {
			Reader reader = 
							Resources.getResourceAsReader("hr/config/SqlMapConfig.xml");
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static EmployeeDAO getInstance(){
		return instance;
	}
	public void insertEmployee(EmployeeTO eto) throws SQLException{
		sqlMap.insert("insertEmployee", eto);
	}
	
	public List selectEmployeeList() throws SQLException{
		return sqlMap.queryForList("selectEmployeeList");
	}
	
	
	
	
	public List selectEmployeeByJobPosition(String jobPosition) throws SQLException{
		return sqlMap.queryForList("selectEmployeeByJobPosition", jobPosition);
	}
	public EmployeeTO selectEmployeeById(String employeeId) throws SQLException{
		return (EmployeeTO)sqlMap.queryForObject("selectEmployeeById", employeeId);
	}
	
	public int updateEmployeeById(EmployeeTO eto) throws SQLException{
		return sqlMap.update("updateEmployeeById", eto);
	}
	
	public List selectEmployeeBySalaryRange(HashMap map) 
								throws SQLException{
		return sqlMap.queryForList("selectEmployeeBySalaryRange", map);
	}
	
	public List selectEmployeeByKeyword(EmployeeTO eto) throws SQLException{
		/*
		 * eto : employeeName이 있으면 이름으로 조회(like 연산자 사용)
		 *         jobPosition이 있으면 직책으로 조회 (=연산자 사용)
		 *         department.departmentName 이 있으면 부서명으로 조회(like 연산자 사용)
		 */
		return sqlMap.queryForList("selectEmployeeByKeyword", eto);
	}
	public List selectEmployeeByNames(HashMap names) throws SQLException{
		return sqlMap.queryForList("selectEmployeeByNames", names);
	}
	
	public static void main(String[] args) throws SQLException{
		DepartmentTO dto = new DepartmentTO();
		dto.setDepartmentId("D0003");
		EmployeeTO eto = 
				new EmployeeTO("00015", "이철", "lc@kosta.org", "과장",200000, "D0002", dto );
		EmployeeDAO dao = EmployeeDAO.getInstance();
		
		ArrayList nameList = new ArrayList();
		nameList.add("이철");
		nameList.add("홍길동");
		nameList.add("김철수");
		
		HashMap map = new HashMap();
		map.put("names", nameList);
		
		List list = dao.selectEmployeeByNames(map);
		for(Object o : list){
			EmployeeTO eto3 = (EmployeeTO)o;
			System.out.println(eto3.getEmployeeId()+" - "+eto3.getEmployeeName());
		}
		
		
		
		
		
		
//		EmployeeTO eto2 = new EmployeeTO();
//		eto2.setEmployeeName("이");
//		eto2.setJobPosition("부장");
//		eto2.setDepartment(new DepartmentTO());
//		eto2.getDepartment().setDepartmentName("영업");
//		List list = dao.selectEmployeeByKeyword(eto2);
//		for(Object o : list){
//			System.out.println(o);
//		}
		//insert
		//dao.insertEmployee(eto);
		//select
//		HashMap map = new HashMap();
////		map.put("start_salary", 500000);
//		map.put("end_salary", 2000000);
//		List list2 = dao.selectEmployeeBySalaryRange(map);
//		System.out.println("---------------월급범위로 조회-------------------");
//		for(Object o : list2){
//			System.out.println(o);
//		}
		
		
		
		
//		update
//		eto.setJobPosition("부장");
//		eto.setSalary(500000);
//		//System.out.println(dao.updateEmployeeById(eto));
//		List list = dao.selectEmployeeList();
//		System.out.println(dao.selectEmployeeById("00001"));
//		System.out.println("--------------------------------------------");
//		//List list = dao.selectEmployeeByJobPosition("직원");
//		for(Object obj : list){
//			System.out.println(obj);
//		}
	}
}













